<?php
namespace app\admin\controller;

use app\common\controller\Admin ; 

class Rule extends Admin
{
    


    //控制器初始化
    public function _initialize()
    {
        parent::_initialize();

        $this->model = model('Rule');
    }
    /**
     * 组织管理 
     */
    public function index()
    {
        $map = array();

        $group = input('group/d', 0 );
        $name = input('name/s', '', 'trim' );

        if( $group ){$map['group'] = $group ;}
        if( $name ){$map['name|title'] = ['like' , '%'.$name.'%'] ;}
       
        // $list = $this->model->field(true)->where($map)->order( 'sort asc ')->select();
        $list = $this->model->field(true)->where($map)->order( 'sort asc ')
        ->paginate(20,false,['query' => request()->param()]);
        $data= array(
            'list' => $list,
            'group' => $group,
            'name' => $name,
            );
        $this->assign($data);
        $this->setMeta('系统管理|权限列表');
        return view();
    }

    /**
     * 添加 or  修改
     * @return [type]           [description]
     */
    public function edit(){




        if(IS_AJAX){
            $data   = $this->request->post();
            if ($data) {
                $result = $this->model->isUpdate(!!$data['id'])->validate(true)->save($data);
                if (false === $result) {
                    return $this->error($this->model->getError() );
                } else {
                    //记录行为
                    // action_log('update_config', 'config', $data['id'], session('user_auth.uid'));
                    return $this->success('更新成功');
                }
            } else {
                return $this->error( $this->model->getError() );
            }  
        }else{
            $id = input('id/d' , 0 );
            $info = $this->model-> get($id);

            $this->assign('info' , $info );
            $this->setMeta('后台管理');
            return view();


            dump($info);
        }

        // if( false ===  $this->Dept->change() ){
        //     return json(['ret'=> 1 , 'msg'=> $this->Dept -> getError() , 'data' => null ]); 
        // }
        
    }


    /**
     * 删除配置  一个删除 和 多个删除
     *  
     */
    public function del() {
        $id = array_unique((array) input('id/a', 0));

        if (empty($id)) {
            return $this->error('请选择要操作的数据!');
        }

        $map = array('id' => array('in', $id));

        if ( $this->model->where($map)->delete() ) {
            return $this->success('删除成功！' );   //$msg = '', $url = null, $data = '', $wait = 3, array $header = []
        } else {
            return $this->error('删除失败！' );    // $msg = '', $url = null, $data = '', $wait = 3, array $header = []
        }
    }

    /**
     * 列表页 表单 改变 ajax 修改
     */
    public function changeeidt($id,$field,$value){
        if(!IS_AJAX){return $this->error( 'not ajax!' );}

        $id = input('id/d',0);
        $field = input('field/s',0);
        $value = input('value/s',0);

        if( ! in_array( $field , ['sort','sort','sort'])){ $this->error("不允许修改[{$field}]字段") ;}

        if( false === $this->model->where('id',$id)->setField( $field , $value )){
            return $this->error( $this->model->getError() );
        }else{
             return $this->success('更新成功');
        }
    }



    


}


